<template>
	<view class="launch-event">
		<!-- #ifndef  H5 -->
		<u-sticky offset-top="0" h5-nav-height="0" bg-color="transparent" style="background-color: #e5fdee">
			<u-navbar :is-back="true" title="发起活动" :title-bold="true" :is-fixed="false" :border-bottom="false"
				title-color="#000"
				:background="{ background: 'rgb(229,253,238)' }" class="custom-navbar"></u-navbar>
		</u-sticky>
		<!-- #endif -->
		<view class="step">
			<view class="steps-wrapper">
				<u-steps :list="stepsList" :current="current" active-color="#0dc160" mode="number"></u-steps>
			</view>
			<view class="step-content">
				<step-one 
					v-if="current === 0"
					:activity-id="activityId"
					:initData="activityData"
					@created="handleActivityCreated"
					@next="current = 1"
				></step-one>
				<step-two
					v-if="current === 1"
					:activity-id="activityId"
					:activity-state="activityState"
				></step-two>
				<step-three
					v-if="current === 2"
					:initData="activityData"
					:activity-state="activityState"
					@jump="handleStepJump"
				></step-three>
				<step-four
					v-if="current === 3"
					:activity-state="activityState"
				></step-four>
			</view>
		</view>
		
	</view>
</template>

<script>
import StepOne from './components/StepOne'
import StepTwo from './components/StepTwo'
import StepThree from './components/StepThree'
import StepFour from './components/StepFour'
import store from '@/store'
import { activityDetail } from '@/api/launchevent'

export default {
	components: {
		StepOne,
		StepTwo, 
		StepThree,
		StepFour
	},
	
	data() {
		return {
			current: 0,
			activityId: null,
			activityData:{},
			activityState: -2, // -2未初始化
			stepsList: [{
				name: '填写信息'
			}, {
				name: '发布'
			}, {
				name: '审核'
			}, {
				name: '完成'
			}],
			showSheet: false,
			show: false,
			mode: 'date',
			calendarType: '',
			actionSheetList: [{
				text: '普通活动',
				subText: '价格上限600元，包括研学活动'
			}, {
				text: '公益活动',
				subText: '价格上限30元'
			}],
		}
	},
	
	onLoad() {
		if (!this.checkLogin()) return;
		const options = this.$Route.query
		console.log(1123,options,this.$Route.query)
		this.activityId = options?.id || null;
		console.log(this.activityId ? `有ID: ${this.activityId}` : '无ID，新建活动');
		
		// this.current = 1;
		
		if (this.activityId) {
			console.log('onload')
			this.loadActivityDetail();
		}
	},
	
	methods: {
		checkLogin() {
			const token = store.getters.token;
			if (!token) {
				uni.showModal({
					title: '提示',
					content: '请先登录后再发起活动',
					showCancel: false,
					success: () => {
						uni.redirectTo({
							url: '/pages/login/login?redirect=/bundle/launchevent/launchevent'
						});
					}
				});
				return false;
			}
			return true;
		},
		async loadActivityDetail() {
      try {
        const res = await activityDetail({ id: this.activityId });
        this.activityState = res.state;
				this.activityData = res
        this.adjustStepByState();
      } catch (e) {
        uni.showToast({ title: '加载失败', icon: 'none' });
      }
    },
		adjustStepByState() {
			switch (this.activityData.state) {
				case 0: // 待发布
					this.current = 1; // StepTwo
					break;
				case 1: // 待审核
					this.current = 2; // StepThree
					break;
				case 2: // 审核通过
					this.current = 3; // StepFour
					break;
				case -1: // 审核失败
					this.current = 2; // StepThree
					break;
				default:
					this.current = 0;
			}
		},
		handleActivityCreated(newId) {
			console.log('add',newId)
			this.activityId = newId;
			this.loadActivityDetail();
		},
		handleStepJump(step) {
			this.current = step;
		},
		showActionSheet() {
			this.showSheet = true
		},
		selectType(index) {
			this.formData.type = this.actionSheetList[index].text
			this.showSheet = false
		},
		calendarChange(e) {
			const time = this.$u.timeFormat(e, 'yyyy-mm-dd')
			if (this.calendarType === 'start') {
				this.formData.startTime = time
			} else {
				this.formData.endTime = time
			}
			this.show = false
		},
		showLocation() {
			// 调用地图选择位置
			uni.chooseLocation({
				success: (res) => {
					this.formData.location = res.address
				}
			})
		},
		afterRead(event) {
			this.fileList.push({
				url: event.file.url
			})
		},
		deletePic(event) {
			this.fileList.splice(event.index, 1)
		},
	}
}
</script>

<style  lang="scss" scoped>
.step {
    .steps-wrapper {
        background-color: #e5fdee;
        padding: 20rpx 0;
    }

    .step-content {
        padding: 20rpx;
    }
}

</style>